package com.example.arithmeticleetcode.learnArithmetic2.sort;

/**
 * @program: arithmetic-leetcode
 * @description:
 * @author: FangZhen
 * @create: 2020-11-05 12:13
 **/
public class BubbleSort2<E extends Comparable<E>> extends Sort<E> {

    @Override
    protected void sort() {
        for (int end = array.length - 1; end > 0; end--) {
            //使用flag进行标记结束
            boolean flag = true;
            for (int begin = 1; begin <= end; begin++) {
//                if (array[begin] > array[begin - 1]) {
//                    flag = false;
//                    int temp = array[begin];
//                    array[begin] = array[begin - 1];
//                    array[begin - 1] = temp;
//                }

                if (cmp(begin, begin - 1) < 0) {
                    flag = false;
                    swap(begin, begin - 1);
                }
            }
            if (flag) {
                break;
            }
        }
    }
}
